<template>
  <div class="admin-layout">
    <el-container class="admin-container">
      <el-aside width="250px" class="admin-sidebar" :class="{ 'sidebar-collapsed': sidebarCollapsed }">
        <div class="admin-logo">
          <img src="@/assets/logo.png" alt="自驾游探索" />
          <h2>管理员控制台</h2>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="admin-menu"
          router
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
          :collapse="sidebarCollapsed"
        >
          <el-menu-item index="/admin">
            <el-icon><DataBoard /></el-icon>
            <span>控制面板</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/users">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/categories">
            <el-icon><Collection /></el-icon>
            <span>类别管理</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/attractions">
            <el-icon><Place /></el-icon>
            <span>景点管理</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/comments">
            <el-icon><ChatDotRound /></el-icon>
            <span>评论管理</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/advertisements">
            <el-icon><Picture /></el-icon>
            <span>广告管理</span>
          </el-menu-item>
          
          <el-menu-item index="/" @click="backToSite">
            <el-icon><Back /></el-icon>
            <span>返回网站</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <el-container class="main-container">
        <el-header class="admin-header">
          <div class="header-left">
            <el-icon class="toggle-sidebar" @click="toggleSidebar">
              <Fold v-if="!sidebarCollapsed" />
              <Expand v-else />
            </el-icon>
            <breadcrumb />
          </div>
          
          <div class="header-right">
            <el-dropdown trigger="click">
              <div class="user-info">
                <el-avatar :size="32" :src="userAvatar">
                  <el-icon><User /></el-icon>
                </el-avatar>
                <span>{{ userStore.user?.username }}</span>
                <el-icon><ArrowDown /></el-icon>
              </div>
              
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="goToProfile">
                    <el-icon><UserFilled /></el-icon>
                    个人资料
                  </el-dropdown-item>
                  <el-dropdown-item @click="logout">
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <el-main class="admin-main">
          <div class="router-view-container">
            <router-view v-slot="{ Component }">
              <transition name="fade" mode="out-in">
                <component :is="Component" />
              </transition>
            </router-view>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { 
  DataBoard, User, Collection, Place, ChatDotRound, Back, 
  Fold, Expand, ArrowDown, UserFilled, SwitchButton, Picture 
} from '@element-plus/icons-vue';
import { useUserStore } from '@/stores/user';
import Breadcrumb from '@/components/admin/Breadcrumb.vue';

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const sidebarCollapsed = ref(false);

const activeMenu = computed(() => {
  return route.path;
});

const userAvatar = computed(() => {
  if (userStore.user?.avatar) {
    return userStore.user.avatar;
  }
  return '';
});

const toggleSidebar = () => {
  sidebarCollapsed.value = !sidebarCollapsed.value;
};

const goToProfile = () => {
  router.push('/user');
};

const logout = () => {
  userStore.logout();
  router.push('/login');
};

const backToSite = () => {
  router.push('/');
};

onMounted(() => {
  // 检查用户是否有管理员权限
  if (userStore.user?.role !== 'admin') {
    ElMessage.error('您没有权限访问管理员页面');
    router.push('/');
  }
});
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
}

.admin-container {
  height: 100%;
  width: 100%;
}

.admin-sidebar {
  background-color: #304156;
  transition: width 0.3s;
  height: 100vh;
  position: relative;
  z-index: 10;
}

.sidebar-collapsed {
  width: 64px !important;
}

.admin-logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  background-color: #263445;
}

.admin-logo img {
  height: 40px;
  margin-right: 10px;
}

.admin-logo h2 {
  color: #fff;
  font-size: 18px;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
}

.sidebar-collapsed .admin-logo h2 {
  display: none;
}

.admin-menu {
  border-right: none;
  height: calc(100vh - 60px);
  overflow-y: auto;
}

.main-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.admin-header {
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 60px;
}

.header-left {
  display: flex;
  align-items: center;
}

.toggle-sidebar {
  font-size: 20px;
  cursor: pointer;
  margin-right: 20px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-info span {
  margin: 0 10px;
}

.admin-main {
  background-color: #f0f2f5;
  padding: 20px;
  flex: 1;
  overflow-y: auto;
  height: calc(100vh - 60px);
}

.router-view-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-sidebar {
    position: fixed;
    z-index: 1000;
    height: 100%;
    transform: translateX(0);
    transition: transform 0.3s;
  }
  
  .sidebar-collapsed .admin-sidebar {
    transform: translateX(-100%);
  }
  
  .admin-main {
    margin-left: 0;
  }
}
</style> 